package com.zhilianbao.leyaogo.view.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bql.convenientlog.CLog;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.constants.IntentConfig;
import com.zhilianbao.leyaogo.constants.PayConfig;

/**
 * Description:余额选择支付方式Dialog
 */
public class BalancePayTypeSelectDialog extends DialogFragment implements View.OnClickListener {

    private int payType;//默认选择type

    private ImageView mWxPay, mAliPay;
    private View mView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            payType = getArguments().getInt(IntentConfig.PAY_TYPE);
        }

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.Dialog);
        mView = LayoutInflater.from(getActivity()).inflate(
                R.layout.dialog_balance_confirm_select_pay_method, null);
        dialog.setContentView(mView);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.windowAnimations = R.style.DialogAnimation;
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
        initView(mView);
        return dialog;
    }

    /**
     * 初始化View
     *
     * @param view
     */
    public void initView(View view) {
        mWxPay = (ImageView) view.findViewById(R.id.iv_wx_pay);
        mAliPay = (ImageView) view.findViewById(R.id.iv_ali_pay);
        mWxPay.setOnClickListener(this);
        mAliPay.setOnClickListener(this);
        view.findViewById(R.id.iv_dismiss).setOnClickListener(this);
        if (payType == 1) {
            mWxPay.setImageResource(R.drawable.pay_wechat_p);
        } else if (payType == 2) {
            mAliPay.setImageResource(R.drawable.pay_alipay_p);
        }
    }

    public View getView() {
        return mView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_dismiss:
                break;
            case R.id.iv_wx_pay://微信
                payType = PayConfig.WX_PAY;
                break;
            case R.id.iv_ali_pay://支付宝
                payType = PayConfig.ALI_PAY;
                //                if(mPayItemClickListener!=null){
                //                    mPayItemClickListener.onItemClick(PayConfig.ALI_PAY);
                //                }
                break;
        }
    }

    /**
     * 选择支付方式，点击事件回调
     */
    public interface OnPayItemClickListener {
        void onItemClick(int type);
    }

    private OnPayItemClickListener mPayItemClickListener;

    public void setPayTypeSelectListener(OnPayItemClickListener listener) {
        mPayItemClickListener = listener;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (mPayItemClickListener != null) {
            mPayItemClickListener.onItemClick(payType);
        }
        CLog.i("hcy", "onDismiss..........");
    }
}
